<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag
                type="list"
                v-model="model.type"
                dictCode="sm_news_type"
                placeholder="请选择类型"
                @change="news(model.type)"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <div v-if="advertype !== 2">
            <!--|| model.type !==2 -->
            <a-col :span="24">
              <a-form-model-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="source">
                <a-input v-model="model.source" placeholder="请输入来源"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="图片类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-radio-group v-model="imgtype">
                  <a-radio :value="1">网络图片 </a-radio>
                  <a-radio :value="2">本地图片</a-radio>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
            <a-col :span="24" v-if="imgtype === 2">
              <a-form-model-item label="图片地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picUrl">
                <j-image-upload  v-model="model.picUrl"  @change="fileListnum"></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="24" v-if="imgtype === 1">
              <a-form-model-item label="图片地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picUrl">
                <a-input v-model="model.picUrl" placeholder="请输入图片"     @change="fileListnum"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="内容类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="urlType">
                <j-dict-select-tag
                  type="list"
                  v-model="model.urlType"
                  dictCode="sm_url_type"
                  placeholder="请选内容类型"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
                <a-input v-model="model.url" placeholder="请输入地址"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
                <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
              </a-form-model-item>
            </a-col>
          </div>
          <!-- <a-col :span="24">
            <a-form-model-item label="时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ctime">
              <a-input v-model="model.ctime" placeholder="请输入时间"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'SmAppNewsForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      fileList :'',
      imgtype: 1, // 图片类型 单选框
      advertype: 0, //广告类型
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/smapp/smAppNews/add',
        edit: '/smapp/smAppNews/edit',
        queryById: '/smapp/smAppNews/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.fileList = this.fileList.slice(-1);
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
      // this.advertype=2
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
       this.advertype=this.model.type
    },
    fileListnum(){

    },

    news(e) {
      this.advertype = e * 1
      // if (this.advertype) {
      //   console.log(121212132);
      //   this.model.urlType = ''
      //   this.model.picUrl = ''
      //   this.model.source = ''
      //   this.model.url = ''
      // }
    },
    submitForm() {
      if (this.advertype === 2) {
        console.log(121212132)
        this.model.urlType = ''
        this.model.picUrl = ''
        this.model.source = ''
        this.model.url = ''
      }
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>